import asyncio
import websocket
import time
import json

SERVER_URL = 'ws://10.240.51.97:8081/cmdset/?hostname='

async def websocket_client(client_name):
    url = f'{SERVER_URL}{client_name}'
    print(url)
    ws = websocket.WebSocket()
    #async with websocket.WebSocket().connect(f'{SERVER_URL}{client_name}') as ws:
        # await websocket.send('Hello, Server!')
        #greeting = await ws.recv()
        # print(f"Server response: {greeting}")
    ws.connect(url)
    # heart
    for i in range(60):
        data = {
                "data":{"hostname":f"{client_name}","status":0},
                "sender":1,
                "sn":"9-9-8"
            }
        ws.send(json.dumps(data))
        res = ws.recv()
        print(res)
        await asyncio.sleep(10)
    #await asyncio.sleep(600)
    #await asyncio.Future()  # This line keeps the connection open

async def run_test(c):
    tasks = []
    for i in range(c):
        client_name = f"test_client_{i}"
        #print(client_name)
        task = asyncio.ensure_future(websocket_client(client_name))
        tasks.append(task)
    #print(task)
    await asyncio.gather(*tasks)

concurrency = 500
#c_time = 5
asyncio.get_event_loop().run_until_complete(run_test(concurrency))
